package operator

import (
	"github.com/go-logr/logr"
	"k8s.io/apimachinery/pkg/runtime"
	nacosv1beta1 "middleware.io/nacos/api/v1beta1"
	myErrors "middleware.io/nacos/pkg/errors"
	"middleware.io/nacos/pkg/service/k8s"
)

type KindClient struct {
	k8sService k8s.Services
	logger     logr.Logger
	scheme     *runtime.Scheme
}

func NewKindClient(logger logr.Logger, s k8s.Services, scheme *runtime.Scheme) *KindClient {
	return &KindClient{
		k8sService: s,
		logger:     logger,
		scheme:     scheme,
	}
}

// ValidationField CR变量验证
func (e *KindClient) ValidationField(nacos *nacosv1beta1.Nacos) {
	setDefaultValue := []func(n *nacosv1beta1.Nacos){
		setDefaultNacosType,
		setDefaultDatabase,
		setDefaultCertification,
	}
	for _, f := range setDefaultValue {
		f(nacos)
	}
}

func setDefaultNacosType(nacos *nacosv1beta1.Nacos) {
	if nacos.Spec.Type == "" {
		nacos.Spec.Type = "standalone"
	}
}

// 设置默认nacos 数据存储类型
func setDefaultDatabase(nacos *nacosv1beta1.Nacos) {
	if nacos.Spec.Database.TypeDatabase == "" {
		nacos.Spec.Database.TypeDatabase = "embedded"
	}

	if nacos.Spec.Database.TypeDatabase == "mysql" {
		if nacos.Spec.Database.MysqlHost == "" {
			nacos.Spec.Database.MysqlHost = MysqlHost
		}
		if nacos.Spec.Database.MysqlUser == "" {
			nacos.Spec.Database.MysqlUser = MysqlUser
		}
		if nacos.Spec.Database.MysqlDb == "" {
			nacos.Spec.Database.MysqlDb = MysqlDB
		}
		if nacos.Spec.Database.MysqlPassword == "" {
			nacos.Spec.Database.MysqlPassword = MysqlPassword
		}
		if nacos.Spec.Database.MysqlPort == "" {
			nacos.Spec.Database.MysqlPort = MysqlPort
		}
	}
}

func setDefaultCertification(nacos *nacosv1beta1.Nacos) {
	// 默认设置认证参数
	if nacos.Spec.Certification.Enabled {
		if nacos.Spec.Certification.Token == "" {
			nacos.Spec.Certification.Token = CertificationToken
		}
		if nacos.Spec.Certification.TokenExpireSeconds == "" {
			nacos.Spec.Certification.TokenExpireSeconds = TokenExpireSeconds
		}
	}
}

func (e *KindClient) EnsureStatefulSet(nacos *nacosv1beta1.Nacos) {
	ss := e.buildStatefulSet(nacos)
	myErrors.EnsureNormal(e.k8sService.CreateOrUpdateStatefulSet(nacos.Namespace, ss))
}

func (e *KindClient) EnsureStatefulSetCluster(nacos *nacosv1beta1.Nacos) {
	ss := e.buildStatefulSet(nacos)
	ss = e.buildStatefulSetCluster(nacos, ss)
	myErrors.EnsureNormal(e.k8sService.CreateOrUpdateStatefulSet(nacos.Namespace, ss))
}

func (e *KindClient) EnsureConfigmap(nacos *nacosv1beta1.Nacos) {
	if nacos.Spec.Config != "" {
		cm := e.buildConfigMap(nacos)
		myErrors.EnsureNormal(e.k8sService.CreateIfNotExistsConfigMap(nacos.Namespace, cm))
	}
}

func (e *KindClient) EnsureService(nacos *nacosv1beta1.Nacos) {
	svc := e.buildService(nacos)
	myErrors.EnsureNormal(e.k8sService.CreateIfNotExistsService(nacos.Namespace, svc))
}

func (e *KindClient) EnsureHeadlessServiceCluster(nacos *nacosv1beta1.Nacos) {
	ss := e.buildService(nacos)
	ss = e.buildHeadlessServiceCluster(ss, nacos)
	myErrors.EnsureNormal(e.k8sService.CreateOrUpdateService(nacos.Namespace, ss))
}

func (e *KindClient) EnsureClientService(nacos *nacosv1beta1.Nacos) {
	ss := e.buildClientService(nacos)
	myErrors.EnsureNormal(e.k8sService.CreateIfNotExistsService(nacos.Namespace, ss))
}
